package edu.unf.cen6940.dao;

import edu.unf.cen6940.domain.Researchkeyword;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Researchkeyword entities.
 * 
 */
@Repository("ResearchkeywordDAO")
@Transactional
public class ResearchkeywordDAOImpl extends AbstractJpaDao<Researchkeyword>
		implements ResearchkeywordDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Researchkeyword.class }));

	/**
	 * EntityManager injected by Spring for persistence unit rms
	 *
	 */
	@PersistenceContext(unitName = "rms")
	private EntityManager entityManager;

	/**
	 * Instantiates a new ResearchkeywordDAOImpl
	 *
	 */
	public ResearchkeywordDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findResearchkeywordByKeywordContaining
	 *
	 */
	@Transactional
	public Set<Researchkeyword> findResearchkeywordByKeywordContaining(String keyword) throws DataAccessException {

		return findResearchkeywordByKeywordContaining(keyword, -1, -1);
	}

	/**
	 * JPQL Query - findResearchkeywordByKeywordContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Researchkeyword> findResearchkeywordByKeywordContaining(String keyword, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findResearchkeywordByKeywordContaining", startResult, maxRows, keyword);
		return new LinkedHashSet<Researchkeyword>(query.getResultList());
	}

	/**
	 * JPQL Query - findResearchkeywordByResearchKeywordId
	 *
	 */
	@Transactional
	public Researchkeyword findResearchkeywordByResearchKeywordId(Integer researchKeywordId) throws DataAccessException {

		return findResearchkeywordByResearchKeywordId(researchKeywordId, -1, -1);
	}

	/**
	 * JPQL Query - findResearchkeywordByResearchKeywordId
	 *
	 */

	@Transactional
	public Researchkeyword findResearchkeywordByResearchKeywordId(Integer researchKeywordId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findResearchkeywordByResearchKeywordId", startResult, maxRows, researchKeywordId);
			return (edu.unf.cen6940.domain.Researchkeyword) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAllResearchkeywords
	 *
	 */
	@Transactional
	public Set<Researchkeyword> findAllResearchkeywords() throws DataAccessException {

		return findAllResearchkeywords(-1, -1);
	}

	/**
	 * JPQL Query - findAllResearchkeywords
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Researchkeyword> findAllResearchkeywords(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllResearchkeywords", startResult, maxRows);
		return new LinkedHashSet<Researchkeyword>(query.getResultList());
	}

	/**
	 * JPQL Query - findResearchkeywordByKeyword
	 *
	 */
	@Transactional
	public Set<Researchkeyword> findResearchkeywordByKeyword(String keyword) throws DataAccessException {

		return findResearchkeywordByKeyword(keyword, -1, -1);
	}

	/**
	 * JPQL Query - findResearchkeywordByKeyword
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Researchkeyword> findResearchkeywordByKeyword(String keyword, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findResearchkeywordByKeyword", startResult, maxRows, keyword);
		return new LinkedHashSet<Researchkeyword>(query.getResultList());
	}

	/**
	 * JPQL Query - findResearchkeywordByPrimaryKey
	 *
	 */
	@Transactional
	public Researchkeyword findResearchkeywordByPrimaryKey(Integer researchKeywordId) throws DataAccessException {

		return findResearchkeywordByPrimaryKey(researchKeywordId, -1, -1);
	}

	/**
	 * JPQL Query - findResearchkeywordByPrimaryKey
	 *
	 */

	@Transactional
	public Researchkeyword findResearchkeywordByPrimaryKey(Integer researchKeywordId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findResearchkeywordByPrimaryKey", startResult, maxRows, researchKeywordId);
			return (edu.unf.cen6940.domain.Researchkeyword) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Researchkeyword entity) {
		return true;
	}
}
